
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui_self_service/mine/pagestate_ss_setting.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_login.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
系统设置
 */
class PageStateSelfServiceSettingLogin extends XPageState{

  PageStateSelfServiceSettingLogin();

  String? pwd;

  @override
  Widget getBodyWidget(BuildContext context) {
    String adminName='${XSharePreferencesUtils.getString(RBConfigs.SP_UserName)} (${XSharePreferencesUtils.getString(RBConfigs.SP_UserCode)})';
    if(adminName.length>21){
      adminName='${adminName.substring(0,21)}...';
    }

    return XWidgetUtils.getLayoutCenter(
        XWidgetUtils.getXLayoutRoundedRect(
            height:840,
            radius_topLeft: 30,
            radius_topRight: 30,
            radius_bottomRight: 30,
            radius_bottomLeft: 30,
            margin: XWidgetUtils.getEdgeInsets(50, 0, 50, 0),
            backgroundColor: Colors.white,
            child:XWidgetUtils.getLayoutColumn(
                children: [
                  XWidgetUtils.getLayoutContainer(
                      width: double.infinity,
                      margin: XWidgetUtils.getEdgeInsets(0, 60, 0, 0),
                      child:XWidgetUtils.getLayoutStack(
                          alignment:AlignmentDirectional.center,
                          children: [
                            XWidgetUtils.getWidgetText(
                                '请输入管理员密码',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 46,
                                    color: XColorUtils.color333333
                                )
                            ),
                            XWidgetUtils.getLayoutPositioned(
                                top: 0,
                                right: 32,
                                child: XWidgetUtils.getWidgetGestureDetector(
                                    child: XImageUtils.getImageWidgetWithAsset(
                                        'ss_close.png',
                                        width: 70,
                                        height: 70
                                    ),
                                    onTap: (){
                                      finishPage();
                                    }
                                )
                            )
                          ]
                      )
                  ),
                  XWidgetUtils.getWidgetText(
                      '管理员',
                      style: XStyleUtils.getTextStyle(
                          fontSize: 34,
                          color: XColorUtils.color999999
                      ),
                      padding: XWidgetUtils.getEdgeInsets(80, 92, 0, 0)
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 16, 80, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetText(
                                adminName,
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 40,
                                    color: XColorUtils.color000000
                                ),
                              ),
                            ),
                            XWidgetUtils.getWidgetGestureDetector(
                                child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                  radius_bottomLeft: 8,
                                  radius_bottomRight: 8,
                                  radius_topRight: 8,
                                  radius_topLeft: 8,
                                  height: 50,
                                  width: 180,
                                  alignment: Alignment.center,
                                  borderColor: RBColors.reabamColor4,
                                  borderWidth: 2,
                                  child: XWidgetUtils.getWidgetText(
                                    '切换管理员',
                                    style: XStyleUtils.getTextStyle(
                                      fontSize: 30,
                                      color: RBColors.reabamColor4,
                                    ),
                                  ),
                                ),
                                onTap: (){
                                  startPage(StatePageSelfServiceLogin(),cleanUntilRetainPageName: '/');
                                }
                            )
                          ]
                      )
                  ),
                  XWidgetUtils.getWidgetText(
                      '密码',
                      style: XStyleUtils.getTextStyle(
                          fontSize: 34,
                          color: XColorUtils.color999999
                      ),
                      padding: XWidgetUtils.getEdgeInsets(80, 39, 0, 0)
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 16, 80, 31),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetTextField(
                                  initialValue: pwd,
                                  isShowInputText:false,
                                  hintText: '请输入密码',
                                  hintTextStyle: XStyleUtils.getTextStyle(
                                      fontSize: 40,
                                      color: XColorUtils.color999999
                                  ),
                                  style: XStyleUtils.getTextStyle(
                                      fontSize: 40,
                                      color: XColorUtils.color000000
                                  ),
                                  onChanged: (res){
                                    pwd=res;
                                  },
                                  onSubmitted: (res){
                                    _submit();
                                  }
                              ),
                            ),
                          ]
                      )
                  ),
                  XWidgetUtils.getWidgetDivider(marginLeft: 40,marginRight: 40),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(70, 102, 70, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetGestureDetector(
                                  child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                      margin: XWidgetUtils.getEdgeInsets(10, 0, 10, 0),
                                      height: 120,
                                      alignment: Alignment.center,
                                      backgroundColor: Colors.white,
                                      borderColor: RBColors.reabamColor4,
                                      borderWidth: 3,
                                      radius_topLeft: 16,
                                      radius_topRight: 16,
                                      radius_bottomRight: 16,
                                      radius_bottomLeft: 16,
                                      child: XWidgetUtils.getWidgetText(
                                        '取消',
                                        style: XStyleUtils.getTextStyle(
                                            fontSize: 46,
                                            color: RBColors.reabamColor4
                                        ),
                                      )
                                  ),
                                  onTap: (){
                                    finishPage();
                                  }
                              ),
                            ),
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetGestureDetector(
                                  child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                      margin: XWidgetUtils.getEdgeInsets(10, 0, 10, 0),
                                      height: 120,
                                      alignment: Alignment.center,
                                      backgroundColor: RBColors.reabamColor4,
                                      borderColor: RBColors.reabamColor4,
                                      borderWidth: 3,
                                      radius_topLeft: 16,
                                      radius_topRight: 16,
                                      radius_bottomRight: 16,
                                      radius_bottomLeft: 16,
                                      child: XWidgetUtils.getWidgetText(
                                        '确认',
                                        style: XStyleUtils.getTextStyle(
                                          fontSize: 46,
                                          color: Colors.white,
                                        ),
                                      )
                                  ),
                                  onTap: (){
                                    _submit();
                                  }
                              ),
                            ),
                          ]
                      )
                  ),
                ]
            )
        )
    );
  }

  void _submit(){
    if (XStringUtils.isEmpty(pwd)) {
      toast("请输入密码");
      return;
    }
    String loginPwd=XSharePreferencesUtils.getString(RBConfigs.SP_loginPwd);

    if(loginPwd!=pwd){
      toast("密码错误");
      return;
    }

    startPage(PageStateSelfServiceSetting(),isFinish: true);

  }

  @override
  Color getXPageBackgroundColor() {
    return XColorUtils.pageMengcengColor;
  }


  @override
  void onStart() {
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {

  }




}